package com.necsv;

import java.util.Date;

import adapter.CustomSimpleCursor;
import adapter.DBObject;
import adapter.DbAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class SqliteActivity extends Activity{
	private static final String TAG = "SqliteActivity";
	private DbAdapter db;
	SimpleCursorAdapter simpleCursorAdapter;
	CustomSimpleCursor mcCustomSimpleCursor;
	ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqlite);
		listView = (ListView) findViewById(R.id.lvSqlite);
		db = new DbAdapter(this);
		db.open();
		Cursor mCursor = db.getAllEntry();
//		if(!mCursor.moveToFirst())
//		{
//			insertDB();
//		}
//		if(mCursor.moveToFirst()) // check cursor not empty
//		{
//			do {
//				String name = mCursor.getString(1);
//				Log.v(TAG, name);
//			} while (mCursor.moveToNext());
//		}
		String []from = new String[]{"_id","name","age"};
		int []to = new int[]{R.id.id_entry,R.id.name_entry, R.id.age_entry};
		
//		simpleCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.sqlite_entry, mCursor, from, to);
		mcCustomSimpleCursor = new CustomSimpleCursor(getBaseContext(), mCursor, from, to);
		listView.setAdapter(mcCustomSimpleCursor);
		db.close();
	}
	
	public void insertDB()
	{
		db.insert(new DBObject("Sonnt",23));
		db.insert(new DBObject("Vinh", 27));
		db.insert(new DBObject("Yuki", 15));
		db.insert(new DBObject("Doremon", 14));
	}
	
	public int insertDBByCurrentTime()
	{
		return (int) db.insert(new DBObject(new Date().getMinutes() + ":" + new Date().getSeconds() ,new Date().getDate()));
	}
	
	public void btn_onClick(View v)
	{
		Button btn = (Button)v;
		switch (btn.getId()) {
		case R.id.btnInstall:
			if(!db.isOpen())
				db.open();
			insertDBByCurrentTime();
			break;
		case R.id.btnRemoveAtSqlite:

			break;
		case R.id.btnFindSqlite:
			
			break;
		case R.id.btnRemoveAllSqlite:
			if(!db.isOpen())
				db.open();
			db.removeAll();
			break;
		case R.id.btnRefreshSqlite:
			
			break;

		default:
			break;
		}
	}
}
